import Vue from 'vue'
import Router from 'vue-router'
import cookie from "js-cookie";
Vue.use(Router);

var route = new Router({
    // mode: 'hash',
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [{
            path: '/',
            name: 'index',
            meta: {
                requireLogin: true // 当前路由需要校验
            },
            component: resolve => require(['@/views/index.vue'], resolve),
        }, {
            path: '/login',
            name: 'login',
            component: resolve => require(['@/views/pub/login.vue'], resolve),
        },
        // 404
        {
            path: '*',
            name: '404',
            component: resolve => require(['@/views/pub/404.vue'], resolve),
        },
    ],
});

//全局路由前置守卫
route.beforeEach((to, from, next) => {
    let token = cookie.get('token');
    if (to.meta.requireLogin) {
    //     if (token === null || token === undefined || token === "undefined") {
    //         next({
    //             path: '/login'
    //         });
    //     } else {
    //         next();
    //     }
        next();

    } else {
        next();
    }
});

//全局路由后置守卫
route.afterEach((to, from) => {
    // console.log(to, from)
})
export default route;